<?php
include "/opt/lampp/htdocs/dom/html/chang.php";

// 面向对象封装数据库操作
class DB{
    public $con;
    function __construct() {
        $this->con = @mysqli_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASSWD,MYSQL_DB,MYSQL_PORT);
        if(mysqli_connect_errno()){
            die("连接MySQL失败:".mysqli_connect_errno()." 错误为:".mysqli_connect_error());
        }
        mysqli_set_charset($this->con,MYSQL_CHARSET);
    }
    function close(){
        mysqli_close($this->con);
    }
    function __destruct(){
        $this->close();
    }
    function command($sql,$ctrl){
         mysqli_query($this->con,$sql);
         $line = mysqli_affected_rows($this->con);
         if($line){
            return $line;
         }else {
            return 0;
         }
    }
    function insert($sql){
        return $this->command($sql,"insert");
    }
    function update($sql){
        return $this->command($sql,"update");
    }
    function delete($sql){
        return $this->command($sql,"delete");
    }
    function select($sql){
        $result = mysqli_query($this->con,$sql);
        if(!$result){
            return 0;
        }
        // 定义一个数组
        $result_list = [];
        // 遍历结果集
        while($row = mysqli_fetch_assoc($result )){
            array_push($result_list,$row);
        }
        return $result_list;
    }
}









?>